<template>
  <view class="calendar_containter">
    <view class="containter">
      <view class="dayun-year-box">
        <view class="dayun-item">
          <view class="dayun-year"></view>
        </view>
        <template v-for="(item,index) in dayun">
          <view class="dayun-item" :key="index" @click="dayunClick(item, index)">
            <view class="dayun-year">{{item.startAge}}岁</view>
            <view class="dayun-year">{{item.startYear}}</view>
          </view>
        </template>
      </view>
      <view class="dayun-box">
        <view class="dayun-item">
          <view class="dayun-info">大运</view>
        </view>
        <template v-for="(item,index) in dayun">
          <view class="dayun-item" :key="index" @click="dayunClick(item, index)">
            <view class="dayun-info" :class="[dayunIndex == index ? 'dayun-select' : '',]">
              {{item.ganZhi}}
            </view>
          </view>
        </template>
      </view>
    </view>
  </view>
</template>
<script>
  import wuxingColor from '@/utils/wuxing.js'
  import {
    EventBus
  } from '@/main.js'
  export default {
    props: {
      result: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        baziResult: this.result,
        dayun: [],
        dayunIndex: 0
      }
    },
    mounted() {
      this.$nextTick(() => {
        // 补全行个数
        let dayunTem = JSON.parse(JSON.stringify(this.baziResult.daYun));
        dayunTem.shift();
        this.dayun = dayunTem;
        // 寻找当前面年份
        let nowYear = new Date().getFullYear();
        // 若不存此项则取默认第一项
        let targetItem = dayunTem[0];
        for (let [index, item] of dayunTem.entries()) {
          if (nowYear >= item.startYear && nowYear <= item.endYear) {
            targetItem = item;
            this.dayunIndex = index;
            break
          }
        }
        this.$emit('change', targetItem);
        EventBus.$emit('dayun', {
          dayunData: targetItem,
          isFirst: true
        })
      })
    },
    methods: {
      // 对应五行信息
      fiveElementInfo(str) {
        let {
          color
        } = wuxingColor.getJiuLongGanzhiWuxingColorInfo(str);
        return `color: ${color}`
      },
      dayunClick(data, index) {
        if (this.dayunIndex == index) return
        this.dayunIndex = index;
        this.$emit('change', data);
        EventBus.$emit('dayun', {
          dayunData: data
        })
      }
    }
  };
</script>

<style lang="scss" scoped>
  .calendar_containter {
    width: 100%;
    box-sizing: border-box;
    font-weight: bold;
    background-color: #eeeeee;
  }
  .dayun-year-box {
    background-color: #cccccc;
  }
  .dayun-box, .dayun-year-box {
    display: flex;
    flex-direction: row;
    box-sizing: border-box;

    .dayun-item {
      display: flex;
      flex: 1 0;
      min-width: 0;
      flex-direction: column;
      align-items: center;
      box-sizing: border-box;

      .dayun-year {
        width: 100%;
        font-size: 20rpx;
        text-align: center;
        white-space: nowrap;
      }

      .dayun-info {
        display: flex;
        flex-direction: row;
        align-items: center;
        padding: 14rpx;
        box-sizing: border-box;
        white-space: nowrap;

        .dayun-ele {
          display: flex;
          flex-direction: row;
          line-height: 44rpx;

          .dayun-ganzhi {
            font-size: 40rpx;
            padding-right: 4rpx;
          }

          .dayun-shishen {
            font-size: 20rpx;
            color: #4B240D;
          }
        }
      }
    }
  }
  .dayun-select {
    background-color: #999999;
  }
</style>